﻿namespace clowncar.Utils
{
    using System;
    using System.IO;
    using System.Reflection;

    static class Resources
    {
        /// <summary>
        /// Extracts an embedded file out of a given assembly.
        /// </summary>
        /// <param name="assemblyName">The namespace of you assembly.</param>
        /// <param name="fileName">The name of the file to extract.</param>
        /// <returns>A stream containing the file data.</returns>
        public static Stream GetEmbeddedFileStream(string assemblyName, string productName, string fileName)
        {
            try
            {
                Assembly a;
                if (assemblyName != null)
                {
                    a = Assembly.Load(assemblyName);
                }
                else
                {
                    a = Assembly.GetExecutingAssembly();
                }

                var str = a.GetManifestResourceStream(productName + "." + fileName);

                if (str == null)
                {
                    throw new Exception("Could not locate embedded resource '" + fileName + "' in assembly '" + assemblyName + "'");
                }

                return str;
            }
            catch (Exception e)
            {
                throw new Exception(assemblyName + ": " + e.Message);
            }
        }
    }
}
